package com.fcat.base.smartapi.lib;

import com.fcat.base.smartapi.service.TokenService;
import com.fcat.base.smartapi.service.impl.TokenServiceImpl;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Test {
    private static void dispBytes(byte[] data) {
        StringBuilder str = new StringBuilder();
        for (byte tmpb: data) {
            str.append(String.format("%02X ", tmpb));
        }
        System.out.println(str.toString());
    }

    public static void main(String[] args) throws Exception {
        System.out.println("Test Make Token ------");
//        System.out.println("Byte.parseByte(\"1234\"): " + Byte.parseByte("12", 16));
//        System.out.println("Byte.valueOf(\"1234\"): " + Byte.valueOf("12", 10));
//        System.out.println("Long.getLong(\"12345678\", 16): " + Long.getLong("12345678", 10));
//        System.out.println("Long.valueOf(\"12345678\", 10): " + Long.valueOf("12345678", 16));
//        System.out.println("Long.parseLong(\"12345678\", 10): " + Long.parseLong("12345678", 10));

//        String tmpstr = "0ABC12DEF3456789";
//        for (int i = tmpstr.length() - 1; i >= 0 ; i--) {
//            System.out.println(tmpstr.charAt(i) + " ");
//        }

        String maskedTBoxKey1Text = "0ABC12DEF3456789";
        String BoxTClassText = "0";     // Toker 类
        String BoxFUnitsText = "100";
        String BoxTSClassText = "0";    // Token子类
//        String BoxFDateText = "2022/10/26";
//        String BoxFTimeText = "15:38:23";
//        String BoxFRandomText = "9";     // 随机
        String BoxFRandomText = "12";     // 随机
        String BoxFDateText = "2022/10/27";
        String BoxFTimeText = "10:26:08";


        String BoxKRNText = "7";    // 密钥修正号， 必须小于9
        String BoxKexpNoText = "100"; // 密钥期限标志， 必须小于256
        String BoxTIText = "123";     // 费率索引， 必须小于256
        String BoxROText = "1";      // 密钥更换标记， 必须小于1
//        String maskedTBoxKey2Text = "1122334455667788";     // 新密钥
        String maskedTBoxKey2Text = "99aabbccddeeff00";     // 新密钥

        String masked36BitText = "FFFFFFFFF";

        boolean mode = false;
        WorkType wt = WorkType.STA;
        byte tclass = Byte.parseByte(BoxTClassText, 10);
        long amount = 0L;
        if (BoxFUnitsText.length() != 0) {
            amount = Long.valueOf(BoxFUnitsText, 10);
        }
        byte tsc = Byte.valueOf(BoxTSClassText, 10);
        //{2022/10/26 15:38:23}
        LocalDateTime idt = LocalDateTime.now();
        if (!BoxFDateText.isEmpty() && !BoxFTimeText.isEmpty()) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
            idt = LocalDateTime.parse(BoxFDateText + " " + BoxFTimeText,df);
        }
        switch (tclass) {
            case 0:
                CreditToken myToken;
                if (BoxFRandomText.length() != 0) {
                    byte rn = Byte.parseByte(BoxFRandomText);
                    myToken = new CreditToken((int) amount, idt, rn, tsc, maskedTBoxKey1Text, wt, mode);
                } else {
                    myToken = new CreditToken((int) amount, idt, tsc, maskedTBoxKey1Text, wt, mode);
                }
                System.out.println("maskedTBoxToken1 -- " + myToken.StrToken);
                System.out.println("TCRC1 -- " + myToken.TCRC);
                System.out.println("FCounter -- " + myToken.Tid);
                System.out.println("FRandom -- " + myToken.RandomNum);
                System.out.println("FDate -- " + myToken.IssueTime);
                System.out.println("FTime -- " + myToken.IssueTime);
                System.out.println("FUnits -- " + myToken.get_amount());
                break;

            case 1:
                //ORIGINAL LINE: SimpleManToken SMToken = new SimpleManToken(tsc, ulong.Parse(this.masked36Bit.Text, NumberStyles.HexNumber), mode);
                SimpleManToken SMToken = new SimpleManToken(
                        tsc,
                        new BigInteger(masked36BitText, 16),
                        mode);
                System.out.println("SMToken:   " +  SMToken.StrToken);
                System.out.println("TCRC --- " + String.format("%04X", SMToken.TCRC));
                break;

            case 2:
                if (tsc == 3)
                {
                    try
                    {
                        //ORIGINAL LINE: KeyChangeToken KChangeToken = new KeyChangeToken(0, byte.Parse(this.tBoxKRN.Text.ToString()), byte.Parse(this.tBoxKexpNo.Text.ToString()), byte.Parse(this.tBoxTI.Text.ToString()), byte.Parse(this.tBoxRO.Text.ToString()), this.maskedTBoxKey2.Text, this.maskedTBoxKey1.Text, mode);
                        KeyChangeToken KChangeToken =
                                new KeyChangeToken(
                                        (byte)0,
                                        Byte.valueOf(BoxKRNText),
                                        Byte.valueOf(BoxKexpNoText),
                                        Byte.valueOf(BoxTIText),
                                        Byte.valueOf(BoxROText),
                                        maskedTBoxKey2Text,
                                        maskedTBoxKey1Text,
                                        mode);
                        if (KChangeToken.xErr < 0)
                        {
                            System.out.println("Token --- "  + KChangeToken.StrToken);
                            System.out.println("Token2 --- "  + KChangeToken.StrTokenKey2);
                            System.out.println("TCRC --- " + String.format("%04X", KChangeToken.TCRC));
                            System.out.println("TCRC2 --- " + String.format("%04X", KChangeToken.TCRC2));
                        }
                        else
                        {
                            System.err.println("KChangeToken  Error:" + KChangeToken.xErr);
                        }
                        break;
                    }
                    catch (RuntimeException e)
                    {
                        System.err.println("请检查输入参数是否完全!");
                        break;
                    }
                }

                ManagementToken ManageToken;
                if (BoxFRandomText.length() != 0)
                {
                    byte rn = Byte.parseByte(BoxFRandomText);
                    ManageToken = new ManagementToken(amount, idt, rn, tsc, maskedTBoxKey1Text, mode);
                }
                else
                {
                    ManageToken = new ManagementToken(amount, idt, tsc, maskedTBoxKey1Text, mode);
                }

                System.out.println("maskedTBoxToken1 -- " + ManageToken.StrToken);
//                this.tBoxTCRC1.Text = String.format("%1$.4X", ManageToken.TCRC);
                System.out.println("TCRC1 -- " + ManageToken.TCRC);
                System.out.println("FCounter -- " + ManageToken.Tid);
                System.out.println("FRandom -- " + ManageToken.RandomNum);
                System.out.println("FDate -- " + ManageToken.IssueTime);
                System.out.println("FTime -- " + ManageToken.IssueTime);
//                this.tBoxFUnits.Text = ManageToken.TransferAmount.toString();
                System.out.println("FUnits -- " + ManageToken.get_amount());

                break;

            case 3:
                break;
        }
/*
        BaseToken bt = new BaseToken();
        byte[] data = new byte[]{(byte) 193,(byte) 170,(byte) 248,(byte) 133,(byte) 180,(byte) 211,(byte) 219,(byte) 242};
        String test =  bt.HexToStrDec(data, (byte) 0);
        System.out.println(test);
*/

        System.out.println("------ Test  MeterKeys -------");
        MeterKeys meterKeys = new MeterKeys();
//        MeterKeys meterKeys = new MeterKeys("12345678903");
        meterKeys.DKGA2();
        dispBytes(meterKeys.getMeterKey());
        System.out.println(meterKeys.getMeterKeyString());


    }
}
